package u.can.i.up.nist;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FileUtils {
	public static void copy(String src, String des) {  
		try {
			List<String> commandAndParameters = new ArrayList<String>(Arrays.asList("cp", "-R", src, des));
			ProcessBuilder builder = new ProcessBuilder();
			builder.redirectErrorStream(true); // This is the important part
			builder.command(commandAndParameters);
			Process proc = builder.start();
			proc.getInputStream().close();
			proc.getErrorStream().close();
			proc.getOutputStream().close();
			proc.waitFor();
		} catch (IOException | InterruptedException e) {
			e.printStackTrace();
		}
	} 
	
	public static int statisticNumberic(String filePath, String targetStr){
		int count = 0;
		try {
			Reader in = new FileReader(filePath);
			int c;
			while ((c = in.read()) != -1) {
				while (c == targetStr.charAt(0)) {
					for (int i = 1; i < targetStr.length(); i++) {
						c = in.read();
						if (c != targetStr.charAt(i)) {
							break;
						}
						if (i == targetStr.length() - 1) {
							count++;
						}
					}
				}
			}
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}
	
	public static List<File> getFileList(String strPath) {
		List<File> fileList = new ArrayList<File>();
		getFileList(strPath, fileList);
		return fileList;
	}
	private static void getFileList(String strPath, List<File> fileList) {  
        File dir = new File(strPath);  
        File[] files = dir.listFiles(); // 该文件目录下文件全部放入数组  
        if (files != null) {  
            for (int i = 0; i < files.length; i++) {  
                String fileName = files[i].getName();  
                if (files[i].isDirectory()) { // 判断是文件还是文件夹  
                    getFileList(files[i].getAbsolutePath(), fileList); // 获取文件绝对路径  
                } else if (fileName.endsWith("apk")) { // 判断文件名是否以.apk结尾  
                    fileList.add(files[i]);  
                } else {  
                    continue;  
                }  
            }  
        }  
    }  
}
